package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.dto.StoreUserDTO;
import com.cjh.common.entity.Store;
import com.cjh.common.entity.User;
import com.cjh.common.mapper.UserMapper;
import com.cjh.common.response.Result;
import com.cjh.common.service.PermissionService;
import com.cjh.common.service.StoreService;
import com.cjh.common.mapper.StoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Set;

/**
 *
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store>
    implements StoreService{
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PermissionService permissionService;

    @Override
    public Result getAllStore(String username) {
        Integer userId = null;
        if(!permissionService.isSuperAdministrator(username)){
            userId = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username)).getUserId();
        }
        List<StoreUserDTO> stores = storeMapper.selectStoreListByShopownerId(userId);
        return new Result().setCode(200).setMessage("操作成功！").setData(stores);
    }

    @Override
    public Result addStore(Store store) {
        storeMapper.insert(store);
        return new Result().setCode(200).setMessage("操作成功！");
    }

    @Override
    public Result updateStore(Store store) {
        storeMapper.updateById(store);
        return new Result().setCode(200).setMessage("操作成功！");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result deleteStoreById(Integer id) {
        try {
            storeMapper.deleteById(id);
            return new Result().setCode(200).setMessage("操作成功！");
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result().setCode(202).setMessage(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result deleteStoreByIdList(List<Integer> list) {
        try {
            storeMapper.deleteBatchIds(list);
            return new Result().setCode(200).setMessage("操作成功！");
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result().setCode(202).setMessage(e.getMessage());
        }
    }

    @Override
    public Result getUserHaveOrBelongToStore(String username) {
        Integer userId = userMapper.selectOne(new QueryWrapper<User>().eq("username", username)).getUserId();
        List<Store> userHaveOrBelongToStore = storeMapper.getUserHaveOrBelongToStore(permissionService.isSuperAdministrator(username),userId);
        return new Result().setCode(200).setMessage("获取成功！").setData(userHaveOrBelongToStore);
    }

    @Override
    public Result getStore() {
        List<Store> stores = storeMapper.selectList(Wrappers.<Store>lambdaQuery().orderByDesc(Store::getIsShow).orderByAsc(Store::getStoreId));
        return new Result().setCode(0).setMessage("获取成功！").setData(stores);
    }
}




